//导入vue和VueRouter包
import Vue from 'vue'
import VueRouter from 'vue-router'
import R00 from '@/components/AXR/R_01/R00'
import R01 from '@/components/AXR/R_01/R01'
import R02 from '@/components/AXR/R_01/R02'
import R03 from '@/components/AXR/R_01/R03'
import login from '@/components/AXR/R_02/login'
import main from '@/components/AXR/R_02/main'
import any from '@/components/AXR/R_02/any'

//调用Vue.use函数，把VueRouter安装为Vue的插件
Vue.use(VueRouter)

//创建路由的实例对象
const router =new VueRouter({
	//routes节点用于定义hash地址与组件之间的对应关系
	//path:路径,component:组件位置
	//path:路径,component:组件位置,children:子路由路径（子路由路径不要以'/'开头）
	//path:路径,redirect:重定向地址
	routes:[
		{path:'/',redirect:'/R01'},
		{
			path:'/R00',
			//默认子路由
			//如果children数组中某个路由规则的path值为'',则这条路由规则为默认子路由
			//redirect:'/R00/R01',
			component:R00,
			children:[
				{path:'',component:R01},
				{path:'R01',component:R01},
				{path:'R02',component:R02},
				{path:'R03',component:R03}
			]
		},
		// {path:'/R01',component:R01},
		// {path:'/R02',component:R02},
		// {path:'/R03',component:R03},
		//path:路径后的'/:参数名'叫做路径参数（在路径参数中，可以通过this.$route.params访问），为动态路由,props:props传参开关
		{path:'/R01/:id',component:R01,props:true},
		{path:'/R02/:id',component:R02,props:true},
		{path:'/R03/:id',component:R03},
		{path:'/Login',component:login},
		{path:'/Main',component:main},
		{path:'/Any',component:any},
	]
})

// router实例对象，声明全局前置导航守卫
// 只要发生了跳转，就会触发beforeEach指定的function回调函数
// 全局前置导航守卫的回调函数中接收3个形参
// to：将要访问路由的信息对象
// from：将要离开的路由的信息对象
// next：调用next()表示放行，允许这次路由导航
router.beforeEach(function(to,from,next){
	//next的参数，
	//next('/R01/1'),表示强制调转到'/R01/1'页面
	//next(false),表示不允许跳转
	//next(),表示直接放行
	console.log(to)
	console.log(from)
	if(from.path === '/Any' && to.path==='/Main'){
		
		//localStorage.getItem('token') 获取token
		//localStorage.setItem('token',value) 设置token
		//token都要以 'Bearer '开头
		//浏览器的本地存储方式有以下三种：
		//cookie、IndexedDB、Storage（sessionStorage、localStorage）
		
		const token = localStorage.getItem('token')
		console.log(to)
		console.log(from)
		
		if(token){
			next()
		} else {
			next('/Login')
		}
	} else {
		next()
	}
	
})
//向外共享路由的实例对象
export default router